package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.qo.ClassinfoQueryObject;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {
    @Autowired
    private IClassinfoService classinfoService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"classinfo:list","班级列表"},logical = Logical.OR)
    public String list(Model model, ClassinfoQueryObject qo){
        PageInfo<Classinfo> pageInfo = classinfoService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Employee> employees = employeeService.selectAll();
        model.addAttribute("qo", qo);
        model.addAttribute("employees", employees);
        return "classinfo/classinfo";
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"classinfo:delete","班级删除"},logical = Logical.OR)
    public JsonResult delete(Long id){
        try {
            classinfoService.delete(id);
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }

    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"classinfo:input","班级编辑"},logical = Logical.OR)
    public String input(Long id,Model model){
        if(id != null){
            Classinfo classinfo = classinfoService.selectByPrimaryKey(id);
            model.addAttribute("classinfo", classinfo);
        }
        return "classinfo/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"classinfo:saveOrUpdate","班级新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Model model,@ModelAttribute("qo") Classinfo classinfo,Long id){
        try {
            if(id != null){
                classinfoService.update(classinfo);
            }else {
                classinfoService.insert(classinfo);
            }
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage(),false);
        }

    }
}
